﻿namespace LogManager.Gui.ViewModels
{
    using LogManager.Core.Infrastructure.Logging;
    using LogManager.Core.Models;
    using LogManager.Gui.Helpers;
    using LogManager.Gui.Views;
    using LogManager.Utils.Unity;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Windows.Input;
    using LogManager.Gui.Factories;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class PlateformViewModel : ViewModelBase<IPlateformView>, IPlateformViewModel
    {
        public IPlateform Plateform { get; private set; }
        public ReadOnlyObservableCollection<IServerViewModel> ServersVm { get; private set; }
        public readonly IMainViewModel MainViewModel;
        public PlateformViewModel(IPlateformView view, IMainViewModel mainViewModel, IPlateform plateform, IViewModelFactory viewModelFactory)
            : base(view)
        {
            this.MainViewModel = mainViewModel;
            this.Plateform = plateform;
            this.ServersVm = Plateform.Servers.GetSyncWrapper(m => viewModelFactory.CreateServerViewModel(m));
        }

        #region Commands

        public ICommand DeletePlateformCommand { get { return new DelegateCommand(OnDeletePlateform); } }
        private void OnDeletePlateform()
        {
            this.Plateform.DeletePlateform();
            Log.Logger.AddInfo("Plateforme supprimée", this.Plateform.Name);
        }

        public ICommand AddServerCommand { get { return new DelegateCommand(OnServerCommand); } }
        private void OnServerCommand()
        {
            var newServer = this.Plateform.AddNewServer();
            this.MainViewModel.View.ExpandAndSelectItem(ServersVm.Single(p => p.Server == newServer));
            Log.Logger.AddInfo("Serveur créé", newServer.Name);
        }

        #endregion
    }
}
